//
//  LCActivityViewController.m
//  EnterpriseCloud
//
//  Created by 东亨 on 2017/7/11.
//  Copyright © 2017年 lichao. All rights reserved.
//

#import "LCActivityViewController.h"
#import "LCWebTableViewCell.h"
#import "LCTimeTableViewCell.h"
#import "CountHeight.h"
#import "ActivityDetailModel.h"
static NSString *cellidentifier = @"LCWebTableViewCell";
static NSString *cellidentifier1 = @"LCTimeTableViewCell";
@interface LCActivityViewController ()<UITableViewDelegate,UITableViewDataSource,UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) IBOutlet UIView *headView;
@property (weak, nonatomic) IBOutlet UILabel *summerLbl;
@property (weak, nonatomic) IBOutlet UILabel *activityType;

@property (strong,nonatomic)NSMutableDictionary *dataDic;
@property (strong,nonatomic)ActivityDetailModel *detailModel;
@end
@implementation LCActivityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setBarTitle:@"报名详情"];
    _dataDic = [[NSMutableDictionary alloc]initWithCapacity:0];
    [self loadCustomeView];
    [self loadData];
}
- (void)loadCustomeView
{
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableHeaderView = _headView;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableView registerNib:[UINib nibWithNibName:@"LCWebTableViewCell" bundle:nil] forCellReuseIdentifier:cellidentifier];
    [_tableView registerNib:[UINib nibWithNibName:@"LCTimeTableViewCell" bundle:nil] forCellReuseIdentifier:cellidentifier1];
}
- (void)loadData
{
    [_dataDic setObject:self.event_id forKey:@"event_id"];
    if (get_sp(@"company_id"))
    {
       [_dataDic setObject:get_sp(@"company_id") forKey:@"company_id"];
    }
    [MDataProvider activityDetailBodyDic:_dataDic sucess:^(ActivityDetailModel *model)
     {
         _detailModel = model;
         _summerLbl.text = model.event_title;
         _activityType.text = model.event_type_name;
         [_tableView reloadData];
     } faile:^(NSString *str)
     {
         [SVProgressHUD showErrorWithStatus:str];
     }];
}
#pragma mark -UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     if (indexPath.row == 0)
     {
         LCWebTableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:cellidentifier];
         cell1.webView.delegate = self;
         [cell1.webView loadHTMLString:_detailModel.event_content baseURL:nil];
         return cell1;
     }
     else
     {
        LCTimeTableViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier:cellidentifier1];
         if ([_state isEqualToString:@"未开始"])
         {
             cell2.titleLbl1.text = @"活动预计开始时间";
             cell2.timelbl1.text = _detailModel.expect_start;
             cell2.titleLbl2.text = @"活动预计结束时间";
             cell2.timeLbl2.text = _detailModel.expect_end;
         }
         else if([_state isEqualToString:@"报名中"])
         {
             cell2.titleLbl1.text = @"活动开始时间";
             cell2.timelbl1.text = _detailModel.event_start;
             cell2.titleLbl2.text = @"活动结束时间";
             cell2.timeLbl2.text = _detailModel.event_end;
         }
         else
         {
             cell2.titleLbl1.text = @"活动开始时间";
             cell2.timelbl1.text = _detailModel.event_start;
             cell2.titleLbl2.text = @"活动结束时间";
             cell2.timeLbl2.text = _detailModel.event_end;
         }
         return cell2;
     }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.row == 0)
    {
        return 380;
    }
    else
    {
        return 114;
    }
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSString *script = [NSString stringWithFormat:
                        @"var script = document.createElement('script');"
                        "script.type = 'text/javascript';"
                        "script.text = \"function ResizeImages() { "
                        "var img;"
                        "var maxwidth=%f;"
                        "for(i=0;i <document.images.length;i++){"
                        "img = document.images[i];"
                        "if(img.width > maxwidth){"
                        "img.width = maxwidth;"
                        "}"
                        "}"
                        "}\";"
                        "document.getElementsByTagName('head')[0].appendChild(script);",SCREEN_WIDTH - 36];
    [webView stringByEvaluatingJavaScriptFromString: script];
    [webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
}
@end
